//
//  NBLayerUtils.m
//  automobiledashboard
//
//  Created by 王建华 on 2025/9/26.
//

#import "NBLayerUtils.h"

@implementation NBLayerUtils

+ (void)addgradintLayerWithFram:(CGRect)layerFrame
                        andView:(UIView *)view
                      andColors:(NSArray *)colorArr
                   andLocations:(nonnull NSArray *)locations {
    
    [NBLayerUtils addgradintLayerWithFram:layerFrame
                                  andView:view
                                andColors:colorArr
                            andStartPoint:CGPointMake(0, 0.5)
                              andEndPoint:CGPointMake(1, 0.5)
                             andLocations:locations];
}

+ (void)addgradintLayerWithFram:(CGRect)layerFrame
                        andView:(UIView *)view
                      andColors:(NSArray *)colorArr
                  andStartPoint:(CGPoint)startP
                    andEndPoint:(CGPoint)endP
                   andLocations:(nonnull NSArray *)locations {
    // 创建一个渐变层
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.frame = CGRectMake(0, 0, layerFrame.size.width, layerFrame.size.height);

    // 设置渐变颜色，从黑色到透明
    NSMutableArray *gColor = [NSMutableArray arrayWithCapacity:colorArr.count];
    for (UIColor *color in colorArr) {
        [gColor addObject:(__bridge id)color.CGColor];
    }
    gradientLayer.colors = gColor;

    // 设置渐变方向，这里设置为水平方向
    gradientLayer.startPoint = startP;
    gradientLayer.endPoint = endP;
    gradientLayer.locations = locations;
    // 将渐变层添加到视图上
    [view.layer insertSublayer:gradientLayer atIndex:0];
}

@end
